﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BaseballGame.Models;
using System.Web.Security;
using BaseballGame.Filters;
using WebMatrix.WebData;
using Microsoft.Web.WebPages.OAuth;
using DotNetOpenAuth.AspNet;

namespace BaseballGame.Controllers
{
    public class ManageLeagueController : Controller
    {
        private game_entitiesDbContext db = new game_entitiesDbContext();
        private UsersContext db1 = new UsersContext();

        int leagueSize = 4; //four players per league
        int numOfLeagues = 2;  //two leagues
        //
        // GET: /ManageLeague/
        public ActionResult Index()
        {
            designTeam();

            return View();

        }

        public void designTeam()
        {
            league league = new league();
            int count = db.players.Count();

            for (int x = 0; leagueSize > x; x++)
            {
                Team team = new Team();
                Player pitcher = new Player();
                Random number = new Random();
                pitcher.player_name = generateName(number);
                pitcher.positionId = 1;
                pitcher = playerInitialize(pitcher, number);
                db.players.Add(pitcher);
                db.SaveChanges(); //add it back if it doesnt work
                team.Pitcher = pitcher;
                team.PitcherId = pitcher.Id;
                ///////////////////////////////////////////

                Player firstbase = new Player();
                firstbase.positionId = 2;
                firstbase.player_name = generateName(number);  
                firstbase = playerInitialize(firstbase, number);
                db.players.Add(firstbase);
                db.SaveChanges();
                team.FirstBase = firstbase;
                team.FirstBaseId = firstbase.Id;
                ///////////////////////////////////////////

                Player secondbase = new Player();
                secondbase.positionId = 3;
                secondbase.player_name = generateName(number);
                secondbase = playerInitialize(secondbase, number);
                db.players.Add(secondbase);
                db.SaveChanges();
                team.SecondBase = secondbase;
                team.SecondBaseId = secondbase.Id;

                /////////////////////////////////////////////
                Player thirdbase = new Player();
                thirdbase.positionId = 4;
                thirdbase.player_name = generateName(number);
                thirdbase = playerInitialize(thirdbase, number);
                db.players.Add(thirdbase);
                db.SaveChanges();
                team.ThirdBase = thirdbase;
                team.ThirdBaseId = thirdbase.Id;

                //////////////////////////////////////////
                Player shortstop = new Player();
                shortstop.positionId = 5;
                shortstop.player_name = generateName(number);
                shortstop = playerInitialize(shortstop, number);
                db.players.Add(shortstop);
                db.SaveChanges();
                team.ShortStop = shortstop;
                team.ShortStopId = shortstop.Id;
                /////////////////////////////////////////
                Player catcher = new Player();
                catcher.positionId = 6;
                catcher.player_name = generateName(number);
                catcher = playerInitialize(catcher, number);
                db.players.Add(catcher);
                db.SaveChanges();
                team.Catcher = catcher;
                team.CatcherId = catcher.Id;
                /////////////////////////////////////////
                Player leftfield = new Player();
                leftfield.positionId = 7;
                leftfield.player_name = generateName(number);
                leftfield = playerInitialize(leftfield, number);
                db.players.Add(leftfield);
                db.SaveChanges();
                team.LeftField = leftfield;
                team.LeftFieldId = leftfield.Id;
                //////////////////////////////////////////
                Player rightfield = new Player();
                rightfield.positionId = 9;
                rightfield.player_name = generateName(number);
                rightfield = playerInitialize(rightfield, number);
                db.players.Add(rightfield);
                db.SaveChanges();
                team.Rightfield = rightfield;
                team.RightfieldId = rightfield.Id;
                ////////////////////////////////////////
                Player centerfield = new Player();
                centerfield.positionId = 8;
                centerfield.player_name = generateName(number);
                centerfield = playerInitialize(centerfield, number);
                db.players.Add(centerfield);
                db.SaveChanges();
                team.CenterField = centerfield;
                team.CenterFieldId = centerfield.Id;
                ////////////////////////////////////////
                Player bfirstbase = new Player();
                bfirstbase.positionId = 2;
                bfirstbase.player_name = generateName(number);
                bfirstbase = playerInitialize(bfirstbase, number);
                db.players.Add(bfirstbase);
                db.SaveChanges();
                team.B_FirstBase = bfirstbase;
                team.B_FirstBaseId = bfirstbase.Id;
                ////////////////////////////////////////
                Player bsecondbase = new Player();
                bsecondbase.positionId = 3;
                bsecondbase.player_name = generateName(number);
                bsecondbase = playerInitialize(bsecondbase, number);
                db.players.Add(bsecondbase);
                db.SaveChanges();
                team.B_SecondBase = bsecondbase;
                team.B_SecondBaseId = bsecondbase.Id;
                /////////////////////////////////////////
                Player bshortstop = new Player();
                bshortstop.positionId = 5;
                bshortstop.player_name = generateName(number);
                bshortstop = playerInitialize(bshortstop, number);
                db.players.Add(bshortstop);
                db.SaveChanges();
                team.B_ShortStop = bshortstop;
                team.B_ShortStopId = bshortstop.Id;
                //////////////////////////////////////////
                Player bthirdbase = new Player();
                bthirdbase.positionId = 4;
                bthirdbase.player_name = generateName(number);
                bthirdbase = playerInitialize(bthirdbase, number);
                db.players.Add(bthirdbase);
                db.SaveChanges();
                team.B_ThirdBase = bthirdbase;
                team.B_ThirdBaseId = bthirdbase.Id;
                ////////////////////////////////////////
                Player bcatcher = new Player();
                bcatcher.positionId = 6;
                bcatcher.player_name = generateName(number);
                bcatcher = playerInitialize(bcatcher, number);
                db.players.Add(bcatcher);
                db.SaveChanges();
                team.B_Catcher = bcatcher;
                team.B_CatcherId = bcatcher.Id;
                //////////////////////////////////////
                Player bleftfield = new Player();
                bleftfield.positionId = 7;
                bleftfield.player_name = generateName(number);
                bleftfield = playerInitialize(bleftfield, number);
                db.players.Add(bleftfield);
                db.SaveChanges();
                team.B_LeftField = bleftfield;
                team.B_LeftFieldId = bleftfield.Id;
                /////////////////////////////////////
                Player brightfield = new Player();
                brightfield.positionId = 9;
                brightfield.player_name = generateName(number);
                brightfield = playerInitialize(brightfield, number);
                db.players.Add(brightfield);
                db.SaveChanges();
                team.B_Midfield = brightfield;
                team.B_MidfieldId = brightfield.Id;
                /////////////////////////////////////
                Player bcenterfield = new Player();
                bcenterfield.positionId = 8;
                bcenterfield.player_name = generateName(number);
                bcenterfield = playerInitialize(bcenterfield, number);
                db.players.Add(bcenterfield);
                db.SaveChanges();
                team.B_CenterField = bcenterfield;
                team.B_CenterFieldId = bcenterfield.Id;
                /////////////////////////////////////
                Player bullpen1 = new Player();
                bullpen1.positionId = 1;
                bullpen1.player_name = generateName(number);
                bullpen1 = playerInitialize(bullpen1, number);
                db.players.Add(bullpen1);
                db.SaveChanges();
                team.BullPen1 = bullpen1;
                team.BullPen1Id = bullpen1.Id;
                /////////////////////////////////////
                Player bullpen2 = new Player();
                bullpen2.positionId = 1;
                bullpen2.player_name = generateName(number);
                bullpen2 = playerInitialize(bullpen2, number);
                db.players.Add(bullpen2);
                db.SaveChanges();
                team.BullPen2 = bullpen2;
                team.BullPen2Id = bullpen2.Id;
                ////////////////////////////////////
                Player bullpen3 = new Player();
                bullpen3.positionId = 1;
                bullpen3.player_name = generateName(number);
                bullpen3 = playerInitialize(bullpen3, number);
                db.players.Add(bullpen3);
                db.SaveChanges();
                team.BullPen3 = bullpen3;
                team.BullPen3Id = bullpen3.Id;
                ////////////////////////////////////
                Player bullpen4 = new Player();
                bullpen4.positionId = 1;
                bullpen4.player_name = generateName(number);
                bullpen4 = playerInitialize(bullpen4, number);
                db.players.Add(bullpen4);
                db.SaveChanges();
                team.BullPen4 = bullpen4;
                team.BullPen4Id = bullpen4.Id;
                ///////////////////////////////////
                Player closer = new Player();
                closer.positionId = 1;
                closer.player_name = generateName(number);
                closer = playerInitialize(closer, number);
                db.players.Add(closer);
                db.SaveChanges();
                team.Closer = closer;
                team.CloserId = closer.Id;


                db.teams.Add(team);
                db.SaveChanges();

                team.Pitcher.teamId = team.Id;
                team.FirstBase.teamId = team.Id;
                team.SecondBase.teamId = team.Id;
                team.ThirdBase.teamId = team.Id;
                team.ShortStop.teamId = team.Id;
                team.Rightfield.teamId = team.Id;
                team.LeftField.teamId = team.Id;
                team.CenterField.teamId = team.Id;
                team.Catcher.teamId = team.Id;
                team.B_Catcher.teamId = team.Id;
                team.B_FirstBase.teamId = team.Id;
                team.B_SecondBase.teamId = team.Id;
                team.B_ThirdBase.teamId = team.Id;
                team.B_ShortStop.teamId = team.Id;
                team.B_CenterField.teamId = team.Id;
                team.B_Midfield.teamId = team.Id;
                team.B_LeftField.teamId = team.Id;
                team.BullPen1.teamId = team.Id;
                team.BullPen2.teamId = team.Id;
                team.BullPen3.teamId = team.Id;
                team.BullPen4.teamId = team.Id;
                team.Closer.teamId = team.Id;


                if (league.team1ID == 0)
                {
                    league.team1ID = team.Id;
                    league.team1 = team;
                }
                else if (league.team2ID == 0)
                {
                    league.team2ID = team.Id;
                    league.team2 = team;
                }
                else if (league.team3ID == 0)
                {
                    league.team3ID = team.Id;
                    league.team3 = team;
                }
                else if (league.team4ID == 0) //all team slots have been filled, add it to the league then save
                {
                    league.team4ID = team.Id;
                    league.team4 = team;

                    db.leagues.Add(league); // all 
                    db.SaveChanges();
                }


            } //end of for loop



        }// end of function


        public string generateName(Random num)
        {
            
            int firstnum = 0;
            int secondnum = 0;
            string fname = "";
            string lname = "";
            string name = "";
            string[] firstname = new string[65] { "Jason" , "Michael", "Juan" , "Brian" , "Javier" ,
            "Robert", "Alex", "Charles", "Victor" , "Mufaro", "Marcos", "Joey" , "Ray" , "Miguel", "Tony", 
            "John", "Josh", "Mark", "Gilbert", "Diego", "Santiago", "Aiden", "Ethan", "Jacob", "Abraham" , "Jack" , "Jayden",
            "Sebastian", "Nick", "Christian", "Daniel", "Benjamin", "Ryan", "Chase", "Rodney",
            "Christopher", "Homer", "Matthew" , "Kevin", "Jose" , "Steven", "Mario", "Lars", "Adrian", "Brad",
            "Ken", "Kenny", "Ronald", "Bill", "Ryu", "Austin", "Gabriel", "Noah", "Noe" , "Logan",
            "Elijah", "Thomas", "Gerardo", "Cameron", "Isaac", "Gavin", "Kyle", "Eric", "Carlos", "Justin"};  //array declaration

            string[] lastname = new string[65] { "Wheeler","Avelar","Rodriguez", "Hamilton", "Ruth", "Beltran",
            "Belkin", "Netgear", "Martins", "Jacobson", "Canseco", "Gonzales", "Edwards", "Robinson", "Williams", "Jordan", "Rodman","Johnson", "Shire", "Kingston",
            "Simpson", "Alvarez", "Morales", "Lesnar", "St.Pierre", "Velasquez", "Silva" , "Penn", "Mier" ,"Couture",
            "Evans", "Pettis", "Miller", "Fitch", "Hendrix", "Sanchez", "Olmos", "Rubalcaba", "Hughes", "Araujo",
            "Smith", "Jones", "Mason", "Moore", "McCoy","White", "Clark", "Lee", "Hall", "Walker",
            "Lewis", "Allen", "Hernandez", "Aguayo", "King", "Cummings", "Young", "Lopez", "Nelson", "Perez",
            "Mitchell", "Turner", "Stewart", "Morgan", "Bell"};

            //firstnum = num.Next(0, 19);
            firstnum = num.Next(firstname.Length);

           
            secondnum = num.Next(lastname.Length);

            
           // secondnum = num.Next(0, 19);
            fname = firstname[firstnum];
            lname = lastname[secondnum];
            name = fname + " " + lname;


            return name;
        }

        public Player playerInitialize(Player player, Random number)
        {
            player.speed = number.Next(1, 100);
            player.power = number.Next(1, 100);
            player.hitting_chance = number.Next(1, 100);
            player.at_bat = 0;
            player.runs = 0;
            player.hits = 0;
            player.runs_batted_in = 0;
            player.home_runs = 0;
            player.balls_on_balls = 0;
            player.strike_outs = 0;
            player.stolen_bases = 0;
            player.left_on_base = 0;
            player.fielding = number.Next(1, 100);
            player.batting_avg = 0;
            player.pitching = number.Next(1, 100);
            player.innings_pitched = 0.0;
            player.runs_allowed = 0;
            player.earned_runs_allowed = 0;
            player.home_runs_allowed = 0;
            player.walks_hit_by_ip = 0.0;
            player.earned_runs_average = 0.0;


            return player;
        }
    }

}
